package crypto

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"io"
)

//AESEncryptStreamReader 对文件流进行AES解密
func AESEncryptStreamReader(key []byte, dst io.Writer, src io.Reader) (int64, error) {
	key = bytes.TrimSpace(key)

	block, err := aes.NewCipher(key)
	if err != nil {
		return 0, err
	}

	stream := cipher.NewOFB(block, key)

	reader := &cipher.StreamReader{S: stream, R: src}

	return io.Copy(dst, reader)
}

//AESEncryptStreamWriter 对文件流进行AES加密
func AESEncryptStreamWriter(key []byte, dst io.Writer, src io.Reader) (int64, error) {
	key = bytes.TrimSpace(key)

	block, err := aes.NewCipher(key)
	if err != nil {
		return 0, err
	}

	stream := cipher.NewOFB(block, key)
	writer := &cipher.StreamWriter{S: stream, W: dst}

	return io.Copy(writer, src)
}
